<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .mirror {
            width: 1200px;
            margin: 50px auto;
            position: relative;
            display: flex;
            justify-content: space-between;
        }

        .left {
            width: 300px;
            background-color: #ddd;
        }

        .left1 {
            width: 300px;
            height: 300px;
        }

        .left img {
            width: 300px;
            height: 300px;
        }

        .cover {
            width: 150px;
            height: 150px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: 0;
            cursor: move;

            opacity: 0;
        }

        .right {
            width: 600px;
            height: 600px;
            background-color: #eee;
            overflow: hidden;
            position: relative;
        }

        .right img {
            width: 1200px;
            height: 1200px;
            position: absolute;
        }
    </style>
</head>

<body>


    <div class="mirror">
        <div class="left">
            <div class="left1">
                <img src="https://img12.360buyimg.com/babel/s320x320_jfs/t1/137619/12/32941/122494/647dd368F072a2e76/3f5a4eabf2ad17f7.jpg!cc_320x320.webp"
                    alt="">
                <div class="cover"></div>
            </div>
            <div class="left2"></div>
        </div>
        <div class="right">
            <img src="https://img12.360buyimg.com/babel/s320x320_jfs/t1/137619/12/32941/122494/647dd368F072a2e76/3f5a4eabf2ad17f7.jpg!cc_320x320.webp"
                alt="">
        </div>
    </div>


    <script>

        // 放大镜
        //     左边和右边的比例必须一致


        // 显示隐藏  cover


        class Mirror {
            constructor(el) {
                this.oBox = document.querySelector(el);
                this.oLeft1 = this.oBox.querySelector('.left1');
                this.oCover = this.oBox.querySelector('.cover');
                this.oLeftImg = this.oBox.querySelector('.left1 img');
                this.oRight = this.oBox.querySelector('.right');
                this.oRightImg = this.oBox.querySelector('.right img');


                this.boxLeft = this.oBox.offsetLeft;
                this.boxTop = this.oBox.offsetTop;
                this.r = this.oCover.offsetWidth / 2;

                this.max = this.oLeft1.clientWidth - this.r * 2;


                this.scale = (this.oRightImg.offsetWidth - this.oRight.clientWidth) / this.max;


                this.showEvent()

                this.mouseEvent()
            }

            // 鼠标移入移除
            showEvent() {
                this.oLeft1.onmouseover = () => {
                    this.oCover.style.opacity = 1;
                }
                this.oLeft1.onmouseout = () => {
                    this.oCover.style.opacity = 0;
                }
            }


            // 鼠标跟随
            mouseEvent() {
                this.oLeft1.onmousemove = e => {
                    let x = e.x - this.boxLeft - this.r;
                    let y = e.y - this.boxTop - this.r;
                    if (x < 0) x = 0
                    if (x > this.max) x = this.max;
                    if (y < 0) y = 0
                    if (y > this.max) y = this.max;

                    // 
                    this.oCover.style.left = x + 'px';
                    this.oCover.style.top = y + 'px';


                    // 右边的图片跟着移动
                    this.oRightImg.style.left = - this.scale * x + 'px';
                    this.oRightImg.style.top = -this.scale * y + 'px';


                }
            }

        }


        new Mirror('.mirror');


    </script>

</body>

</html>